package com.hp.bon.sgw.wh.channel.groupConsistency.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @PROJECT_NAME: SGW_WH2.1_TMALL
 * @DESCRIPTION:
 * @USER: YouQi
 * @DATE: 2021/5/8 14:35
 */
public class HttpClientUtils {

	private static final Logger _log = LoggerFactory.getLogger(HttpClientUtils.class);

	public static String postHttpStr(String jsonBody, String httpUrl) {
		_log.info("===========http url===============" + httpUrl);
		String resp = "";
		_log.info("===========http Req Msg===============" + jsonBody);
		PostMethod post = new PostMethod(httpUrl);
		// 请求头
		try {
			RequestEntity requestEntity = new StringRequestEntity(jsonBody, "application/json", "UTF-8");
			post.setRequestEntity(requestEntity);
			HttpClient client = new HttpClient();

			client.getHttpConnectionManager().getParams().setConnectionTimeout(5 * 1000);
			try {
				client.executeMethod(post);
			} catch (HttpException e) {
				_log.info("===========http call timeout===============" + httpUrl);
				return "";
			} catch (IOException e) {
				_log.info("===========http call timeout session_id:");
				return "";
			}
			try {

				InputStream bodyReader = post.getResponseBodyAsStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(bodyReader,"UTF-8"));
				StringBuffer stringBuffer = new StringBuffer();
				String result = "";
				while ((result = br.readLine()) != null) {
					stringBuffer.append(result);
				}
				resp = stringBuffer.toString();
				resp =HttpClientUtils.getUTF8StringFromGBKString(resp);
				bodyReader.close();
				_log.info("===========http Resp Msg===============" + resp);
				// resp = post.getResponseBodyAsString();
			} catch (IOException e) {
				_log.info("===========webservice call timeout===============" + httpUrl);
				return "";
			}
		} catch (UnsupportedEncodingException e) {
			_log.info("===========webservice call timeout===============" + httpUrl);
			e.printStackTrace();
		}
		return resp;
	}

	public static String getUTF8StringFromGBKString(String gbkStr) {
		try {
			return new String(getUTF8BytesFromGBKString(gbkStr), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			throw new InternalError();
		}
	}

	public static byte[] getUTF8BytesFromGBKString(String gbkStr) {
		int n = gbkStr.length();
		byte[] utfBytes = new byte[3 * n];
		int k = 0;
		for (int i = 0; i < n; i++) {
			int m = gbkStr.charAt(i);
			if (m < 128 && m >= 0) {
				utfBytes[k++] = (byte) m;
				continue;
			}
			utfBytes[k++] = (byte) (0xe0 | (m >> 12));
			utfBytes[k++] = (byte) (0x80 | ((m >> 6) & 0x3f));
			utfBytes[k++] = (byte) (0x80 | (m & 0x3f));
		}
		if (k < utfBytes.length) {
			byte[] tmp = new byte[k];
			System.arraycopy(utfBytes, 0, tmp, 0, k);
			return tmp;
		}
		return utfBytes;
	}

}
